
#import "TorrentDetailsViewController.h"
#import "TorrentFileListViewController.h"
#import "TorrentPeerListViewController.h"
#import "TorrentDescAniDBViewController.h"
#import "UserDetailsViewController.h"
#import "ServerListViewController.h"
#import "TorrentMagicViewController.h"

@implementation TorrentDetailsViewController

//
// torrent info
//
@synthesize torrentInfo														= _torrentInfo;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;
	
	//
	// setup title
	//
	self.title																= NSLocalizedStringFromTable(@"Torrent Details", @"TorrentDetailsViewController", nil);
	self.navigationItem.rightBarButtonItem                                  = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshTorrentDetails:)] autorelease];
    
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.torrentInfo														= nil;
	
	[super dealloc];
}

//
// refresh
//
- (void)refreshTorrentDetails:(id)sender
{
	id getTorrentDetailsFinished = ^void(GetTorrentDetailsRequest* httpRequest)
	{
		[ActivityView removeView];
		if(!httpRequest.error && httpRequest.response.statusCode == 200)
		{
			self.torrentInfo.size											= httpRequest.torrentInfo.size;
			self.torrentInfo.uploadDate										= httpRequest.torrentInfo.uploadDate;
			self.torrentInfo.seederCount									= httpRequest.torrentInfo.seederCount;
			self.torrentInfo.leecherCount									= httpRequest.torrentInfo.leecherCount;
			self.torrentInfo.upRate											= httpRequest.torrentInfo.upRate;
			self.torrentInfo.downRate										= httpRequest.torrentInfo.downRate;
			self.torrentInfo.magicExpire									= httpRequest.torrentInfo.magicExpire;
			self.torrentInfo.torrentDescription								= httpRequest.torrentInfo.torrentDescription;
			self.torrentInfo.aniDB											= httpRequest.torrentInfo.aniDB;
			self.torrentInfo.unread											= YES;
			[[DataCenter sharedDataCenter] checkAddTorrent:self.torrentInfo];
			[self.tableView reloadData];
		}
		else
		{
			NSString* message												= [httpRequest errorDescription];
			NSString* title													= NSLocalizedStringFromTable(@"Get Torrent Details Failed", @"TorrentDetailsViewController", nil);
			[UIAlertView alertViewWithTitle:title message:message cancel:NSLocalizedString(@"OK", nil)];
		}
	};
	[ActivityView showActivityView:NSLocalizedStringFromTable(@"Getting Torrent Details", @"TorrentDetailsViewController", nil) networking:YES];
	[[NexusClient sharedClient] getTorrentDetails:self.torrentInfo.torrentId callback:getTorrentDetailsFinished];
}

//
// did load
//
- (void)viewDidLoad
{
	[super viewDidLoad];
	if(![self.torrentInfo isKindOfClass:[UserTorrentInfo class]])
		return;
	
	TorrentInfo* savedInfo													= [[DataCenter sharedDataCenter] torrentInfoFromId:self.torrentInfo.torrentId];
	if(savedInfo)
	{
		self.torrentInfo.size												= savedInfo.size;
		self.torrentInfo.uploadDate											= savedInfo.uploadDate;
		self.torrentInfo.upRate												= savedInfo.upRate;
		self.torrentInfo.downRate											= savedInfo.downRate;
		self.torrentInfo.magicExpire										= savedInfo.magicExpire;
		self.torrentInfo.torrentDescription									= savedInfo.torrentDescription;
		self.torrentInfo.aniDB												= savedInfo.aniDB;
	}
	else
	{
		[self refreshTorrentDetails:nil];
	}
}

//
// will appear
//
- (void)viewWillAppear:(BOOL)animated
{
	if(self.tableView.indexPathForSelectedRow)
		[self.tableView reloadData];
	
	[super viewWillAppear:animated];
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 5;
}

//
// get title
//
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
	return section == 0 ? self.torrentInfo.title : nil;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	return section == 0 ? 5 : (section == 1 ? 2 : (section == 2 ? 2 : (section == 3 ? 2 : 2)));
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	UITableViewCell* theCell												= nil;
	if(indexPath.section  == 0)
	{
		BOOL magicRow														= indexPath.row == 3 || indexPath.row == 4;
		theCell																= [tableView dequeueReusableCellWithIdentifier:magicRow ? @"d>" : @"v1"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:magicRow ? @"d>" : @"v1"] autorelease];
			
		if(indexPath.row == 0)
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Category", @"TorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.category;
		}
		else if(indexPath.row == 1)
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Upload Date", @"TorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.uploadDate;
		}
		else if(indexPath.row == 2)
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Size", @"TorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.size;
		}
		else if(indexPath.row == 3)
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Magic Type", @"TorrentDetailsViewController", nil);
			if(self.torrentInfo.downRate.length && self.torrentInfo.upRate.length)
				theCell.detailTextLabel.text								= [NSString stringWithFormat:@"%@ / %@", self.torrentInfo.upRate, self.torrentInfo.downRate];
			else if(!self.torrentInfo.downRate.length && self.torrentInfo.upRate.length)
				theCell.detailTextLabel.text								= NSLocalizedStringFromTable(self.torrentInfo.upRate, @"TorrentDetailsViewController", nil);
			else
				theCell.detailTextLabel.text								= NSLocalizedStringFromTable(@"---", @"TorrentDetailsViewController", nil);
		}
		else if(indexPath.row == 4)
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Magic Expire", @"TorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.magicExpire.length ? self.torrentInfo.magicExpire : @"---";
		}
		
		theCell.accessoryType												= magicRow ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
		if(!magicRow)
			theCell.selectionStyle											= UITableViewCellSelectionStyleNone;
	}
	else if(indexPath.section == 1)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"d>"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"d>"] autorelease];
		
		theCell.textLabel.text												= indexPath.row == 0 ? NSLocalizedStringFromTable(@"Description", @"TorrentDetailsViewController", nil) : NSLocalizedStringFromTable(@"AniDB", @"TorrentDetailsViewController", nil);;
		theCell.accessoryType												= UITableViewCellAccessoryDisclosureIndicator;
	}
	else if(indexPath.section == 2)
	{
		if(indexPath.row == 0)
		{
			theCell															= [tableView dequeueReusableCellWithIdentifier:@"d>"];
			if(!theCell)
				theCell														= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"d>"] autorelease];
			
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"File List", @"TorrentDetailsViewController", nil);
		}
		else
		{
			theCell															= [tableView dequeueReusableCellWithIdentifier:@"v1>"];
			if(!theCell)
				theCell														= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1>"] autorelease];
			
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Peer List", @"TorrentDetailsViewController", nil);;
			theCell.detailTextLabel.text									= self.torrentInfo.leecherCount >=0 && self.torrentInfo.seederCount >= 0 ? [NSString stringWithFormat:@"%d/%d", self.torrentInfo.seederCount, self.torrentInfo.leecherCount] : nil;
		}
		
		theCell.accessoryType												= UITableViewCellAccessoryDisclosureIndicator;
	}
	else if(indexPath.section == 3)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"d>"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"d>"] autorelease];
		
		if(indexPath.row)
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Torrent (https tracker)", @"TorrentDetailsViewController", nil);
		else
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Torrent (http tracker)", @"TorrentDetailsViewController", nil);
		
		theCell.accessoryType												= UITableViewCellAccessoryDisclosureIndicator;
	}
	else if(indexPath.section == 4)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"d"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"d"] autorelease];
		
		if(indexPath.row)
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Copy DL Address (https tracker)", @"TorrentDetailsViewController", nil);
		else
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Copy DL Address (http tracker)", @"TorrentDetailsViewController", nil);
		
		theCell.accessoryType												= UITableViewCellAccessoryNone;
		theCell.textLabel.textAlignment										= UITextAlignmentCenter;
	}
	
	return theCell;
}

//
// select row
//
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
	if(indexPath.section == 0)
	{
		TorrentMagicViewController* torrentMagicViewController				= [[[TorrentMagicViewController alloc] init] autorelease];
		torrentMagicViewController.torrentInfo								= self.torrentInfo;
		[self.navigationController pushViewController:torrentMagicViewController animated:YES];
	}
	else if(indexPath.section == 1)
	{
		if(indexPath.row == 0)
		{
			TorrentDescAniDBViewController* torrentDescAniDBViewController	= [[[TorrentDescAniDBViewController alloc] init] autorelease];
			torrentDescAniDBViewController.torrentInfo						= self.torrentInfo;
			torrentDescAniDBViewController.showDescription					= YES;
			[self.navigationController pushViewController:torrentDescAniDBViewController animated:YES];
		}
		else if(indexPath.row == 1)
		{
			TorrentDescAniDBViewController* torrentDescAniDBViewController	= [[[TorrentDescAniDBViewController alloc] init] autorelease];
			torrentDescAniDBViewController.torrentInfo						= self.torrentInfo;
			torrentDescAniDBViewController.showDescription					= NO;
			[self.navigationController pushViewController:torrentDescAniDBViewController animated:YES];
		}
	}
	else if(indexPath.section == 2)
	{
		if(indexPath.row == 0)
		{
			TorrentFileListViewController* torrentFileListViewController	= [[[TorrentFileListViewController alloc] init] autorelease];
			torrentFileListViewController.torrentInfo						= self.torrentInfo;
			[self.navigationController pushViewController:torrentFileListViewController animated:YES];
		}
		else if(indexPath.row == 1)
		{
			TorrentPeerListViewController* torrentPeerListViewController	= [[[TorrentPeerListViewController alloc] init] autorelease];
			torrentPeerListViewController.torrentInfo						= self.torrentInfo;
			[self.navigationController pushViewController:torrentPeerListViewController animated:YES];
		}
	}
	else if(indexPath.section == 3)
	{
		ServerListViewController* serverListViewController					= [[[ServerListViewController alloc] init] autorelease];
		serverListViewController.torrentInfo								= self.torrentInfo;
		serverListViewController.useHttpsTracker							= indexPath.row == 1;
		[self.navigationController pushViewController:serverListViewController animated:YES];
	}
	else if(indexPath.section == 4)
	{
		[tableView deselectRowAtIndexPath:indexPath animated:YES];
		[[UIPasteboard generalPasteboard] setString:[[NexusClient sharedClient] torrentDownloadAddress:self.torrentInfo passKey:[[DataCenter sharedDataCenter] passKey] httpsMode:indexPath.row == 1]];
	}
}

@end
